<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增数据 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <FreeDataDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';

  import { BasicTable, useTable, TableAction, BasicColumn } from '/@/components/Table';
  import { getFreeDataList, removeFreeData } from '/@/api/free/data';
  import { getTableDetail } from '/@/api/free/table';

  import { useDrawer } from '/@/components/Drawer';
  import FreeDataDrawer from './FreeDataDrawer.vue';
  import { buildFormScheme, buildSearchFormScheme, buildColumns } from './freedata.data';
  import { useRoute } from 'vue-router';

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, FreeDataDrawer, TableAction },
    setup() {
      const route = useRoute();
      const tableName = route.path.replace('/freeData/', '');
      console.log(tableName);
      const freeProp = reactive({
        tableInfo: {},
        formScheme: [],
      });
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, setColumns, setProps, deleteTableDataRecord }] = useTable({
        api: getFreeDataList,
        immediate: false,
        columns: [],
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        rowKey: 'id',
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      getTableDetail(tableName).then((res) => {
        freeProp.tableInfo = res;
        freeProp.formScheme = buildFormScheme(res.columns);
        // 构造搜索条件
        const searchFormScheme = buildSearchFormScheme(res.columns);
        // 构造标题
        setProps({
          title: res.tableLabel + '列表',
          searchInfo: {
            tableName: res.tableName,
          },
          formConfig: {
            labelWidth: 120,
            schemas: searchFormScheme,
          },
          useSearchForm: searchFormScheme.length > 0,
        });
        // 构造表格 列信息
        const columns: BasicColumn[] = buildColumns(res.columns);
        setColumns(columns);

        reload();
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          freeProp,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          freeProp,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        const tableName = freeProp.tableInfo.tableName;
        removeFreeData(tableName, record.id).then(() => {
          deleteTableDataRecord(record.id);
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
